package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.KingBdCangw;
import com.ruoyi.system.service.IKingBdCangwService;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.KingBdCang;
import com.ruoyi.system.service.IKingBdCangService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 仓库Controller
 * 
 * @author ruoyi
 * @date 2021-04-26
 */
@RestController
@RequestMapping("/system/cang")
public class KingBdCangController extends BaseController
{
    @Autowired
    private IKingBdCangService kingBdCangService;
    @Autowired
    private IKingBdCangwService kingBdCangwService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询仓库列表
     */
    @PreAuthorize("@ss.hasPermi('system:cang:list')")
    @GetMapping("/list")
    public TableDataInfo list(KingBdCang kingBdCang)
    {
        startPage();
        List<KingBdCang> list = kingBdCangService.selectKingBdCangList(kingBdCang);
        return getDataTable(list);
    }

    /**
     * 导出仓库列表
     */
    @PreAuthorize("@ss.hasPermi('system:cang:export')")
    @Log(title = "仓库", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(KingBdCang kingBdCang)
    {
        List<KingBdCang> list = kingBdCangService.selectKingBdCangList(kingBdCang);
        ExcelUtil<KingBdCang> util = new ExcelUtil<KingBdCang>(KingBdCang.class);
        return util.exportExcel(list, "cang");
    }

    /**
     * 获取仓库详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:cang:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(kingBdCangService.selectKingBdCangById(id));
    }

    /**
     * 新增仓库
     */
    @PreAuthorize("@ss.hasPermi('system:cang:add')")
    @Log(title = "仓库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KingBdCang kingBdCang)
    {
        return toAjax(kingBdCangService.insertKingBdCang(kingBdCang));
    }

    /**
     * 修改仓库
     */
    @PreAuthorize("@ss.hasPermi('system:cang:edit')")
    @Log(title = "仓库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KingBdCang kingBdCang)
    {
        return toAjax(kingBdCangService.updateKingBdCang(kingBdCang));
    }

    /**
     * 删除仓库
     */
    @PreAuthorize("@ss.hasPermi('system:cang:remove')")
    @Log(title = "仓库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(kingBdCangService.deleteKingBdCangByIds(ids));
    }

    /**
     * 获取仓库list
     */
    @CrossOrigin
    @PostMapping("/getCangList")
    @ResponseBody
    public String getCang(@RequestBody KingBdCang kingBdCang2){
        if(redisCache.getCacheObject("Cang"+kingBdCang2.getfId())!=null) {
          return  redisCache.getCacheObject("Cang"+kingBdCang2.getfId());
        }else{
          redisCache.setCacheObject("Cang"+kingBdCang2.getfId(),kingBdCangService.getCangList2(kingBdCang2));
            return kingBdCangService.getCangList2(kingBdCang2);
        }
       //
    }
}
